package com.medical.literature.visualization.service.impl;

import com.medical.literature.visualization.dto.KeywordStatisticsRequest;
import com.medical.literature.visualization.dto.KeywordStatisticsResponse;
import com.medical.literature.visualization.dto.LiteratureTrendRequest;
import com.medical.literature.visualization.dto.LiteratureTrendResponse;
import com.medical.literature.visualization.dto.TrendItem;
import com.medical.literature.visualization.entity.KeywordStatistics;
import com.medical.literature.visualization.mapper.KeywordStatisticsMapper;
import com.medical.literature.visualization.service.VisualizationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 可视化服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class VisualizationServiceImpl implements VisualizationService {
    
    private final KeywordStatisticsMapper keywordStatisticsMapper;

    @Override
    public KeywordStatisticsResponse getKeywordStatistics(KeywordStatisticsRequest request) {
        // 查询关键词统计信息
        List<KeywordStatistics> statisticsList = keywordStatisticsMapper.selectKeywordStatistics(
                request.getUserId(),
                request.getStartDate(),
                request.getEndDate(),
                request.getCategory(),
                request.getLimit()
        );
        
        // 转换为响应对象
        KeywordStatisticsResponse response = new KeywordStatisticsResponse();
        response.setTotal(statisticsList.size());
        
        List<KeywordStatisticsResponse.KeywordStatItem> items = statisticsList.stream()
                .map(stat -> {
                    KeywordStatisticsResponse.KeywordStatItem item = new KeywordStatisticsResponse.KeywordStatItem();
                    item.setKeyword(stat.getKeyword());
                    item.setCount(stat.getCount());
                    item.setWeight(stat.getWeight() != null ? stat.getWeight().doubleValue() : 0.0);
                    item.setCategory(stat.getCategory());
                    return item;
                })
                .collect(Collectors.toList());
        
        response.setItems(items);
        
        log.info("获取关键词统计信息成功，用户ID: {}, 返回数量: {}", request.getUserId(), items.size());
        return response;
    }

    @Override
    public LiteratureTrendResponse getLiteratureTrend(LiteratureTrendRequest request) {
        // 查询文献趋势数据
        List<TrendItem> trendList = keywordStatisticsMapper.selectLiteratureTrendItems(
                request.getUserId(),
                request.getStartDate(),
                request.getEndDate()
        );
        
        // 转换为响应对象
        LiteratureTrendResponse response = new LiteratureTrendResponse();
        response.setTotal(trendList.size());
        
        List<LiteratureTrendResponse.TrendItem> items = trendList.stream()
                .map(trend -> {
                    LiteratureTrendResponse.TrendItem item = new LiteratureTrendResponse.TrendItem();
                    item.setTimePoint(trend.getTimePoint());
                    item.setCount(trend.getCount());
                    // 这里可以添加关键词列表逻辑
                    if (trend.getKeywords() != null) {
                        item.setKeywords(Arrays.asList(trend.getKeywords()));
                    } else {
                        item.setKeywords(null);
                    }
                    return item;
                })
                .collect(Collectors.toList());
        
        response.setItems(items);
        
        log.info("获取文献趋势数据成功，用户ID: {}, 返回数量: {}", request.getUserId(), items.size());
        return response;
    }
}